<?php

  session_start();
  include "config.php";
  include "ALproduct.php";

  if(isset($_SESSION["user"]))
  {
    include "$header";

?>

<SCRIPT TYPE="text/javascript">

var a = 0;

  function checkOrder(j)
  {
    var total = 0;

    if(document.productsList.orderbox.length > 0)
    {
      for(var i=0; i < document.productsList.orderbox.length; i++)
      {
        if(document.productsList.orderbox[i].checked)
        {
          total = total + 1;
          document.productsList.oProducts.disabled = false;
        }
        if(total > 10)
        {
          alert("You may only select up to 10 products for ordering.") 
          document.productsList.orderbox[j].checked = false ;
          break;
        }
      }
    }
    if(total == 0)
    {
      document.productsList.oProducts.disabled = true;
      if(a == 1)
      {
        if(document.productsList.orderbox.checked)
        {
          document.productsList.oProducts.disabled = false;
        }
      }
    }
  }

</SCRIPT>

<?php

    include "$body";

?>

<div style="position:absolute; top:0px; left:1px; border:0;">
  <span class="prevPage">Enquiry About Products:Search > </span>
  <span class="page">Search Results</span> 
</div>

<?php
  
    include "$H_eap";

?>

<FORM name="productsList" action="" method="get">

<input class="link_off" type="submit" value="Record Customer Enquiry Problem" name="problem" onmouseover="className='link_on'" onmouseout="className='link_off'" />
<p>

<?php

    if(isset($_SESSION["eap_1"]) && isset($_SESSION["eap_2"]))
    {
      if($_SESSION["eap_1"] == "0")
      {
        echo "<b>NO RESULTS</b>";
      }
	  else
      {
        echo "<b>SEARCH RESULTS FOR: description '<u>" . $_SESSION["eap_description"] . "</u>', department '<u>" . $_SESSION["eap_department"] . "</u>', brand '<u>" . $_SESSION["eap_brand"] . "</u>', size '<u>" . $_SESSION["eap_size"] . "</u>', colour '<u>" . $_SESSION["eap_colour"] . "</u>', price '$<u>" . $_SESSION["eap_price1"] . "." . $_SESSION["eap_price2"] . "</u>'</b>";
      }
      $j = showProducts();
	  if($_SESSION["eap_2"] == "0")
      {
        echo "<b>NO RELATED PRODUCTS</b>";
      }
	  else
      {
        echo "<b>OTHER RELATED PRODUCTS</b>";
      }
      showRelatedProducts($j);
	  unset($_SESSION["eap_description"]);
	  unset($_SESSION["eap_brand"]);
	  unset($_SESSION["eap_size"]);
	  unset($_SESSION["eap_colour"]);
	  unset($_SESSION["eap_price1"]);
	  unset($_SESSION["eap_price2"]);
	  unset($_SESSION["eap_1"]);
	  unset($_SESSION["eap_2"]);
    }
    else if(isset($_GET["problem"]))
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  window.location.href = 'recordCustomerEnquiryProblem.php';";
      echo "</SCRIPT>";
    }
    else if(isset($_GET["oProducts"]))
    {
      $_SESSION["orderProducts"] = $_GET["orderbox"];
	  echo "<SCRIPT language='Javascript'>";
      echo "  window.location.href = 'orderProducts.php';";
	  echo "</SCRIPT>";
    }
    else
    {
      echo "<SCRIPT language='Javascript'>";
      echo "  window.location.href = 'enquiryAboutProducts.php';";
      echo "</SCRIPT>";
    }

?>

<input class="link_off" type="submit" value="Order" name="oProducts" onmouseover="className='link_on'" onmouseout="className='link_off'" />

<SCRIPT language="JavaScript">
  productsList.oProducts.disabled = true;
</SCRIPT>

</FORM>

</CENTER>

</BODY>

<?php

    include "$footer";
  }
  else
  {
    session_destroy();
    echo "<SCRIPT language='Javascript'>";
    echo "  parent.window.location.href = 'index.php';";
    echo "</SCRIPT>";
  }

  function showProducts()
  {

?>

<p>

<TABLE class="eapDetails" cellspacing="2" cellpadding="2">
  <TR>
    <TD class="header">
      Product No.
    </TD>
    <TD class="header">
      Brand
    </TD>
    <TD class="header">
      Size
    </TD>
    <TD class="header">
      Colour
    </TD>
    <TD class="header">
      Description
    </TD>
    <TD class="header">
      Price AU$
    </TD>
    <TD class="header">
      Department
    </TD>
    <TD class="header">
      Stock level
    </TD>
    <TD class="header">
      Order
    </TD>
  </TR>

<?php

    if($_SESSION["eap_1"] != "0")
    {
      $products = $_SESSION["eap_1"];
      $j = 0;
      for($i=0; $i != sizeof($products); $i++)
      {
      
?>

  <TR>
    <TD class="details" align="center">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $products[$i]; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo findDepName($products[$i-5]); ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $stock = findStockLevel($products[$i-5]); ?>
    </TD>
    <TD class="details" align="center">
      <?php
        if($stock > 0)
        {
      ?>
      <input type="checkbox" class="checkbox" id="orderbox" name="orderbox[]" value="<?php echo $products[$i-5]; ?>" onClick="checkOrder(<?php echo $j; $j++; ?>)">
	  <?php
          echo "<SCRIPT language='Javascript'>";
          echo "  a++;";
          echo "</SCRIPT>";
        }
        else
        {
      ?>
      &nbsp;
      <?php
        }
      ?>
    </TD>
  </TR>

<?php

      }
    }

?>

</TABLE>

<p>

<?php
    return $j;
  }

  function showRelatedProducts($j)
  {

?>

<p>

<TABLE class="eapDetails" cellspacing="2" cellpadding="2">
  <TR>
    <TD class="header">
      Product No.
    </TD>
    <TD class="header">
      Brand
    </TD>
    <TD class="header">
      Size
    </TD>
    <TD class="header">
      Colour
    </TD>
    <TD class="header">
      Description
    </TD>
    <TD class="header">
      Price AU$
    </TD>
    <TD class="header">
      Department
    </TD>
    <TD class="header">
      Stock level
    </TD>
    <TD class="header">
      Order
    </TD>
  </TR>

<?php

    if($_SESSION["eap_2"] != "0")
    {
      $products = $_SESSION["eap_2"];
      for($i=0; $i != sizeof($products); $i++)
      {
      
?>

  <TR>
    <TD class="details" align="center">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details">
      <?php echo $products[$i]; $i++; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $products[$i]; ?>
    </TD>
    <TD class="details" align="center">
      <?php echo findDepName($products[$i-5]); ?>
    </TD>
    <TD class="details" align="center">
      <?php echo $stock = findStockLevel($products[$i-5]); ?>
    </TD>
    <TD class="details" align="center">
      <?php
        if($stock > 0)
        {
      ?>
      <input type="checkbox" class="checkbox" id="orderbox" name="orderbox[]" value="<?php echo $products[$i-5]; ?>" onClick="checkOrder(<?php echo $j; $j++; ?>)" />
	  <?php
          echo "<SCRIPT language='Javascript'>";
          echo "  a++;";
          echo "</SCRIPT>";
        }
        else
        {
      ?>
      &nbsp;
      <?php
        }
      ?>
    </TD>
  </TR>

<?php

      }

    }

?>

</TABLE>

<p>

<?php

  }

?>